package com.rongxun.hbwallet.ui.activities;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.rongxun.base.MvpActivity;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.constants.IntentKey;
import com.rongxun.hbwallet.model.bean.CandyTask;
import com.rongxun.hbwallet.mvp.contract.IMoreTask;
import com.rongxun.hbwallet.mvp.presenter.MoreTaskPresenter;
import com.rongxun.hbwallet.ui.adapter.TaskAdapter;
import com.rongxun.hbwallet.ui.adapter.TaskContentAdapter;
import com.rongxun.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者     庄丰泽
 * 创建时间   2018/8/16 11:31
 * 描述	      获取更多糖果
 */

public class MoreTaskActivity extends MvpActivity<IMoreTask.IView, MoreTaskPresenter> implements IMoreTask.IView {
    @BindView(R.id.toolbar_title)
    TextView     mToolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar      mToolbar;
    @BindView(R.id.moretask_rv)
    RecyclerView mRv;
    @BindView(R.id.toolbar_back)
    ImageView    mToolbarBack;
    private DelegateAdapter mDelegateAdapter;

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showLoading(boolean show) {

    }

    @Override
    public void showError(String msg, String errorCode) {

    }

    @Override
    public void checkPermissionsSuccessful(String... permissions) {

    }

    @Override
    public void release() {

    }

    @NonNull
    @Override
    public MoreTaskPresenter createPresenter() {
        return new MoreTaskPresenter(this);
    }

    @Override
    protected void init() {
        StatusBarUtil.setToolBarPaddingTop(mToolbar, this);
        mToolbarTitle.setText(getString(R.string.candy));
        mToolbarBack.setVisibility(View.VISIBLE);
        List<CandyTask> mCanTasks = getIntent().getParcelableArrayListExtra(IntentKey.MORETASKS);
        initAdapter(mCanTasks);
    }

    private void initAdapter(List<CandyTask> mCanTasks) {


        VirtualLayoutManager virtualLayoutManager = new VirtualLayoutManager(this);
        mRv.setLayoutManager(virtualLayoutManager);

        final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        viewPool.setMaxRecycledViews(7, 20);
        mRv.setRecycledViewPool(viewPool);

        mDelegateAdapter = new DelegateAdapter(virtualLayoutManager, false);
        mRv.setAdapter(mDelegateAdapter);

        List<DelegateAdapter.Adapter> mAdaters = new ArrayList<>();

        List<CandyTask> mBaseTask = new ArrayList<>();
        List<CandyTask> mDailyTasks = new ArrayList<>();

        for (CandyTask mCanTask : mCanTasks) {
            if (mCanTask.getTodayLimit() == null) {
                mBaseTask.add(mCanTask);
            } else {
                mDailyTasks.add(mCanTask);
            }
        }


        TaskAdapter taskAdapter = new TaskAdapter(this, new LinearLayoutHelper(0), 1);
        TaskContentAdapter taskContentAdapter = new TaskContentAdapter(this, new LinearLayoutHelper(0), mDailyTasks);
        TaskAdapter taskAdapter2 = new TaskAdapter(this, new LinearLayoutHelper(0), 1);
        TaskContentAdapter taskContentAdapter2 = new TaskContentAdapter(this, new LinearLayoutHelper(0), mBaseTask);

        mAdaters.add(taskAdapter);
        mAdaters.add(taskContentAdapter);
        mAdaters.add(taskAdapter2);
        mAdaters.add(taskContentAdapter2);

        mDelegateAdapter.setAdapters(mAdaters);
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_moretask;
    }


    @OnClick(R.id.toolbar_back)
    public void onViewClicked() {
        onBackPressed();
    }
}
